package service

import (
	"fmt"
	"gitee.com/viwii/vconf/db/dao"
	"gitee.com/viwii/vconf/db/model"
	"github.com/ant0ine/go-json-rest/rest"
)

type NullReply struct {

}

//{"error":"Error 1062: Duplicate entry 'APP-测试-DAS-v2021-03-22' for key 'release_uindex'"}

type ErrReply struct {
	Error string `json:"error"`
}

type Service struct {

}

func (s *Service) ListApps(w rest.ResponseWriter, r *rest.Request) {
	dao := dao.GetDao()
	apps ,err:=dao.ListApps()
	if err!=nil{
		fmt.Println()
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	w.WriteJson(&apps)
}
func (s *Service) CreateApp(w rest.ResponseWriter, r *rest.Request) {
	dao := dao.GetDao()

	app := model.App{}
	err := r.DecodeJsonPayload(&app)

	_ ,err=dao.CreateApp(app.AppName,app.Description)
	if err!=nil{
		fmt.Println()
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	w.WriteJson(&NullReply{})
}

func (s *Service) DeleteApp(w rest.ResponseWriter, r *rest.Request) {
	dao := dao.GetDao()

	r.ParseForm()
	appName := r.Form.Get("appName")

	err:=dao.DeleteApp(appName)
	if err!=nil{
		fmt.Println()
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	w.WriteJson(&NullReply{})
}


func (s *Service) Clusters(w rest.ResponseWriter, r *rest.Request) {

	dao := dao.GetDao()
	r.ParseForm()
	name := r.Form.Get("appName")
	apps ,err:=dao.ListClusters(name)
	if err!=nil{
		fmt.Println()
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	w.WriteJson(&apps)
}

func (s *Service)DeleteCluster(w rest.ResponseWriter, r *rest.Request) {
	dao := dao.GetDao()

	r.ParseForm()
	appName := r.Form.Get("appName")
	clusterName := r.Form.Get("clusterName")

	err := dao.DeleteCluster(appName,clusterName)
	if err!=nil{
		fmt.Println()
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	w.WriteJson(&NullReply{})
}

func (s *Service) CreateCluster(w rest.ResponseWriter, r *rest.Request) {
	dao := dao.GetDao()

	cluster := model.Cluster{}
	err := r.DecodeJsonPayload(&cluster)

	_ ,err =dao.CreateCluster(cluster.AppName,cluster.ClusterName,cluster.Description)
	if err!=nil{
		fmt.Println()
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	w.WriteJson(&NullReply{})
}

func (s *Service) Namespaces(w rest.ResponseWriter, r *rest.Request) {

	dao := dao.GetDao()
	r.ParseForm()
	appName := r.Form.Get("appName")
	clusterName := r.Form.Get("clusterName")
	apps ,err:=dao.ListNamespaces(appName,clusterName)
	if err!=nil{
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	w.WriteJson(&apps)
}

func (s *Service) AddNamespaces(w rest.ResponseWriter, r *rest.Request) {
	dao := dao.GetDao()

	namespace := model.Namespace{}
	err := r.DecodeJsonPayload(&namespace)
	if err != nil {
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	_ ,err=dao.CreateNamespace(namespace.AppName,namespace.ClusterName,namespace.NamespaceName,namespace.Format,namespace.Description)
	if err!=nil{
		fmt.Println()
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	w.WriteJson(&NullReply{})
}


func (s *Service) UpdateConfig(w rest.ResponseWriter, r *rest.Request) {
	dao := dao.GetDao()

	namespace := model.Namespace{}
	err := r.DecodeJsonPayload(&namespace)
	if err != nil {
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}


	err=dao.UpdateConfig(namespace.AppName,namespace.ClusterName,namespace.NamespaceName,namespace.Value)
	if err!=nil{
		fmt.Println()
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	w.WriteJson(&NullReply{})
}

func (s *Service) ListReleaseHistory(w rest.ResponseWriter, r *rest.Request) {
	dao := dao.GetDao()

	r.ParseForm()

	appName := r.Form.Get("appName")
	clusterName := r.Form.Get("clusterName")
	namespaceName := r.Form.Get("namespaceName")


	relase,err:=dao.ListReleaseHistory(appName,clusterName,namespaceName)
	if err!=nil{
		fmt.Println()
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	w.WriteJson(relase)
}

func (s *Service) ReleaseConfig(w rest.ResponseWriter, r *rest.Request) {
	dao := dao.GetDao()

	release := model.Release{}
	err := r.DecodeJsonPayload(&release)
	if err != nil {
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	//relase,err:=dao.ListReleaseHistory(release.AppName,release.ClusterName,release.NamespaceName)

	err =dao.ReleaseConfig(release.AppName,release.ClusterName,release.NamespaceName,release.Tag,release.Comment)
	if err!=nil{
		fmt.Println()
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	w.WriteJson(&NullReply{})
}

func (s *Service) DeleteNamespace(w rest.ResponseWriter, r *rest.Request) {
	dao := dao.GetDao()

	r.ParseForm()

	appName := r.Form.Get("appName")
	clusterName := r.Form.Get("clusterName")
	namespaceName := r.Form.Get("namespaceName")

	//relase,err:=dao.ListReleaseHistory(release.AppName,release.ClusterName,release.NamespaceName)


	err :=dao.DeleteNamespace(appName,clusterName,namespaceName)
	if err!=nil{
		fmt.Println()
		w.WriteJson(&ErrReply{Error: err.Error()})
		return
	}

	w.WriteJson(&NullReply{})
}